<script setup lang="ts">
import { onLoad } from '@dcloudio/uni-app'
import CustomNavbar from './components/CustomNavBar.vue'
import CategoryPanel from './components/CategoryPanel.vue'
import HotPanel from './components/HotPanel.vue'
import PageSkeleton from './components/PageSkeleton.vue'
import { getHomeBannerAPI, getHomeCategoryAPI, getHomeHotAPI } from '@/services/home'
import type { BannerItem, CategoryItem, HotItem } from '@/types/home'
import { ref } from 'vue'
import type { XtxGuessInstance } from '@/types/component'

onLoad(() => {
  getHomeBannerData()
  getCategoryData()
  hotpanelData()
})

// 首页轮播图数据
const bannerList = ref<BannerItem[]>([])
const getHomeBannerData = async () => {
  const res = await getHomeBannerAPI()
  bannerList.value = res.result
}

// 首页分类数据
const categoryList = ref<CategoryItem[]>([])
const getCategoryData = async () => {
  const res = await getHomeCategoryAPI()
  categoryList.value = res.result
}

// 最热推荐数据
const hotpanelList = ref<HotItem[]>([])
const hotpanelData = async () => {
  const res = await getHomeHotAPI()
  hotpanelList.value = res.result
}

// 获取猜你喜欢组件实例
const guessRef = ref<XtxGuessInstance>()

// 滚动容器触底事件 调用子组件的加载更多事件
const onScrolltolower = async () => {
  console.log('触底了')
  guessRef.value?.getMore()
}

// 是否加载中标记
const isLoading = ref(false)
</script>

<template>
  <view class="viewport">
    <!-- 自定义导航栏 -->
    <CustomNavbar />
    <!-- 滚动容器 -->
    <scroll-view
      enable-back-to-top
      refresher-enabled
      class="scroll-view"
      scroll-y
      @scrolltolower="onScrolltolower"
    >
      <PageSkeleton v-if="isLoading" />
      <template v-else>
        <!-- 轮播图 -->
        <XtxSwiper :list="bannerList" />
        <!-- 分类 -->
        <CategoryPanel :list="categoryList" />
        <!-- 推荐 -->
        <HotPanel :list="hotpanelList" />
        <!-- 猜你喜欢 -->
        <XtxGuess ref="guessRef" />
      </template>
    </scroll-view>
  </view>
</template>

<style lang="scss">
page {
  background-color: #f7f7f7;
  height: 100%;
  overflow: hidden;
}

.viewport {
  height: 100%;
  display: flex;
  flex-direction: column;
}

.scroll-view {
  flex: 1;
  overflow: hidden;
}
</style>
